package dao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

import bean.MaintainInfo;
import util.DBUtil;


public class MaintainDao {
   PreparedStatement pstmt=null;
   DBUtil db=new DBUtil();
   Connection conn=db.open();
   ResultSet rs=null;


 public void addMain(MaintainInfo maintain){
	 String Insert="insert into maintainInfo(maintainer,keyboarder,company_name,contents,main_time) values(?,?,?,?,?)";
      try {
		pstmt=conn.prepareStatement(Insert);
		pstmt.setString(1, maintain.getMaintainer());
		pstmt.setString(2, maintain.getKeyboarder());
		pstmt.setString(3, maintain.getCompanyName());
		pstmt.setString(4, maintain.getContents());
		pstmt.setDate(5, new java.sql.Date((maintain.getMaindate()).getTime()));
		pstmt.executeUpdate();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		db.close();
	}
 }
 
 //模糊查询
 public ArrayList<MaintainInfo> getAllMain(){
	  String FINDBYID="select * from maintainInfo ";
	  ArrayList<MaintainInfo> list=new ArrayList<MaintainInfo>();
	  db.open();
	  try {
		pstmt=conn.prepareStatement(FINDBYID);
		rs=pstmt.executeQuery();
		while(rs.next()){
			int id=rs.getInt(1);
			String maintainer=rs.getString(2);
			String keyboarder=rs.getString(3);
			String comName=rs.getString(4);
			String contents=rs.getString(5);
			Date mdate=rs.getDate(6);
			
			MaintainInfo minfo=new MaintainInfo();
			minfo.setId(id);
			minfo.setMaintainer(maintainer);
			minfo.setKeyboarder(keyboarder);
			minfo.setCompanyName(comName);
			minfo.setContents(contents);
			minfo.setMaindate(mdate);
			
			list.add(minfo);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return list;
 }
  public  ArrayList<MaintainInfo> findByDate(Date mdate1,Date mdate2){
	  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      String s1=format.format(mdate1);
      String s2=format.format(mdate2);
	  ArrayList<MaintainInfo> list=new ArrayList<MaintainInfo>();
	  String sql="select * from maintainInfo where main_time between "+s1+" and "+s2;
	  db.open();
	  
	  try {
		  pstmt=conn.prepareStatement(sql);
		  pstmt.executeQuery();
		while(rs.next()){
			int id=rs.getInt(1);
			String maintainer=rs.getString(2);
			String keyboarder=rs.getString(3);
			String comName=rs.getString(4);
			String contents=rs.getString(5);
			Date mdate=rs.getDate(6);
			
			MaintainInfo minfo=new MaintainInfo();
			minfo.setId(id);
			minfo.setMaintainer(maintainer);
			minfo.setKeyboarder(keyboarder);
			minfo.setCompanyName(comName);
			minfo.setContents(contents);
			minfo.setMaindate(mdate);
			
			list.add(minfo);
		  }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return list;
	  
  }
  
//删除维修记录
 public void deleteMain(int id){
	 String dele="delete from maintainInfo where id="+id;
	 db.open();
	 try {
		 pstmt=conn.prepareStatement(dele);
		 pstmt.executeUpdate();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
 }
 
 
 
}